package qianfeng.com.meijiadaka;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import butterknife.BindView;
import butterknife.ButterKnife;
import qianfeng.com.meijiadaka.adapter.shop_details.ShopCategoryHeadRecyclerAdapter;
import qianfeng.com.meijiadaka.bean.detail.HeadCommoditiesBean;

public class HeadCategoryActivity extends AppCompatActivity {

    private String CATEGORY_PATH = "http://mall.naildaka.com/vapi/v3/mall/commodity/search?pageNo=1&keyword=&categoryId=";
    private int categoryId;
    @BindView(R.id.head_category_title)
    TextView mTitle;
    @BindView(R.id.head_category_recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.head_category_back)
    ImageView mBack;
    private HeadCommoditiesBean cb;
    ShopCategoryHeadRecyclerAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_head_category);
        ButterKnife.bind(this);
        categoryId = getIntent().getIntExtra("categroyId",0);
        initView();
        loadData();
    }

    private void initView() {
        cb = new HeadCommoditiesBean();
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        GridLayoutManager manager = new GridLayoutManager(this,2);
        mRecyclerView.setLayoutManager(manager);
        mTitle.setText(getIntent().getStringExtra("title"));

    }

    private void loadData() {
        RequestParams rp = new RequestParams(CATEGORY_PATH+categoryId);
        rp.setConnectTimeout(3000);
        x.http().get(rp, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                cb = new Gson().fromJson(result, HeadCommoditiesBean.class);
                //适配器
                adapter = new ShopCategoryHeadRecyclerAdapter(cb,HeadCategoryActivity.this);
                mRecyclerView.setAdapter(adapter);
//                mRecyclerView.addItemDecoration(new RecyclerViewDivider(HeadCategoryActivity.this,GridLayoutManager.HORIZONTAL));
//                mRecyclerView.addItemDecoration(new RecyclerViewDivider(HeadCategoryActivity.this,GridLayoutManager.VERTICAL));
                adapter.setOnItemClickListener(new ShopCategoryHeadRecyclerAdapter.OnItemClickListener() {
                    @Override
                    public void onClick(int index) {
                        Intent intent = new Intent(HeadCategoryActivity.this, CommodityDtailsActivity.class);
                        intent.putExtra("commodityId", cb.getCommodities().get(index).getCommodityId());
                        startActivity(intent);
                    }
                });

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e("onError: ", "错误1");
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Log.e("onSuccess: ", "完成1");
            }

            @Override
            public void onFinished() {
                Log.e("onFinished: ", "结束1");
            }
        });

    }



}
